/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import dao.CuaHangDAO;
import dao.LoaiNguoiDungDAO;
import dao.NguoiDungDAO;
import java.awt.Component;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import org.apache.log4j.Logger;
import pojo.CuaHang;
import pojo.LoaiNguoiDung;
import pojo.NguoiDung;

/**
 *
 * @author 0
 */
public class DanhSachNhanVienGUI extends javax.swing.JPanel {

    /**
     * Creates new form DanhSachNhanVienGUI
     */
    private List<NguoiDung> nguoiDungs;
    private static Logger logger = Logger.getLogger(DanhSachNhanVienGUI.class);

    public DanhSachNhanVienGUI() throws Exception {
        initComponents();
        FormatControls();
        AutoLoad();
    }

    private void FormatControls() {
        Utility.FormatNumberStringTextfield(txtMaNhanVien);
        Utility.FormatNumberStringTextfield(txtCMND);
    }

    private void AutoLoad() throws Exception {
        nguoiDungs = (List<NguoiDung>) NguoiDungDAO.TimKiemNhanVien(null, "", "", "", null, null, MainGUI.nguoiDung);
        Utility.CreateColumnsFromDB(NguoiDung.class, nguoiDungs, tbDanhSachNhanVien, new String[]{"Mã nhân viên", "Tên nhân viên", "Username", "CMND", "Điện thoại", "Địa chỉ", "Ngày sinh", "Loại người dùng", "Cửa hàng"}, new String[]{"password"});

        List<LoaiNguoiDung> loaiNguoiDungs = (List<LoaiNguoiDung>) LoaiNguoiDungDAO.LayDanhSach(LoaiNguoiDung.class, MainGUI.nguoiDung);
        loaiNguoiDungs.remove(0);
        Collections.sort(loaiNguoiDungs, new Comparator<LoaiNguoiDung>() {
            @Override
            public int compare(LoaiNguoiDung o1, LoaiNguoiDung o2) {
                return o1.getTenLoaiNguoiDung().compareTo(o2.getTenLoaiNguoiDung());
            }
        });
        List<CuaHang> cuaHangs = (List<CuaHang>) CuaHangDAO.LayDanhSach(CuaHang.class, MainGUI.nguoiDung);
        Collections.sort(cuaHangs, new Comparator<CuaHang>() {
            @Override
            public int compare(CuaHang o1, CuaHang o2) {
                return o1.getTenCuaHang().compareTo(o2.getTenCuaHang());
            }
        });
        Utility.SetDataSourceInCombo(loaiNguoiDungs, cboLoaiNhanVien);
        Utility.SetDataSourceInCombo(cuaHangs, cboCuaHang);

        cboCuaHang.setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (value instanceof CuaHang) {
                    CuaHang cuaHang = (CuaHang) value;
                    setText(cuaHang.getTenCuaHang());
                }
                return this;
            }
        });
        cboCuaHang.setSelectedIndex(-1);

        cboLoaiNhanVien.setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (value instanceof LoaiNguoiDung) {
                    LoaiNguoiDung loaiNguoiDung = (LoaiNguoiDung) value;
                    setText(loaiNguoiDung.getTenLoaiNguoiDung());
                }
                return this;
            }
        });
        cboLoaiNhanVien.setSelectedIndex(-1);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtMaNhanVien = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtTenNhanVien = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtCMND = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        cboLoaiNhanVien = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        cboCuaHang = new javax.swing.JComboBox();
        btnTimKiem = new javax.swing.JButton();
        btnThem = new javax.swing.JButton();
        btnSua = new javax.swing.JButton();
        btnXoa = new javax.swing.JButton();
        btnClear = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbDanhSachNhanVien = new org.jdesktop.swingx.JXTable();
        btnTaiKhoan = new javax.swing.JButton();

        jLabel1.setText("Mã nhân viên");

        jLabel2.setText("Tên nhân viên");

        jLabel3.setText("Username");

        jLabel4.setText("CMND");

        jLabel5.setText("Loại nhân viên");

        jLabel6.setText("Cửa hàng");

        btnTimKiem.setText("Tìm kiếm");
        btnTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTimKiemActionPerformed(evt);
            }
        });

        btnThem.setText("Thêm");
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        btnSua.setText("Sửa");
        btnSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSuaActionPerformed(evt);
            }
        });

        btnXoa.setText("Xoá");
        btnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaActionPerformed(evt);
            }
        });

        btnClear.setText("Clear");
        btnClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearActionPerformed(evt);
            }
        });

        tbDanhSachNhanVien.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tbDanhSachNhanVien.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbDanhSachNhanVienMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbDanhSachNhanVien);

        btnTaiKhoan.setText("Tài khoản");
        btnTaiKhoan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTaiKhoanActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addGap(19, 19, 19)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtUsername, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                            .addComponent(txtMaNhanVien)
                            .addComponent(txtTenNhanVien))
                        .addGap(55, 55, 55)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4)
                            .addComponent(jLabel6))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtCMND)
                            .addComponent(cboLoaiNhanVien, 0, 207, Short.MAX_VALUE)
                            .addComponent(cboCuaHang, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(122, 122, 122)
                        .addComponent(btnTimKiem)
                        .addGap(18, 18, 18)
                        .addComponent(btnThem)
                        .addGap(18, 18, 18)
                        .addComponent(btnSua)
                        .addGap(18, 18, 18)
                        .addComponent(btnXoa)
                        .addGap(18, 18, 18)
                        .addComponent(btnClear)
                        .addGap(18, 18, 18)
                        .addComponent(btnTaiKhoan)))
                .addGap(0, 40, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(txtMaNhanVien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txtCMND, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtTenNhanVien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(cboLoaiNhanVien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(cboCuaHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnTimKiem)
                    .addComponent(btnThem)
                    .addComponent(btnSua)
                    .addComponent(btnXoa)
                    .addComponent(btnClear)
                    .addComponent(btnTaiKhoan))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTimKiemActionPerformed
        try {
            // TODO add your handling code here:
            Integer maNhanVien = null;
            if (!txtMaNhanVien.getText().isEmpty()) {
                maNhanVien = Integer.parseInt(txtMaNhanVien.getText());
            }
            CuaHang cuaHang = (CuaHang) cboCuaHang.getModel().getSelectedItem();
            LoaiNguoiDung loaiNguoiDung = (LoaiNguoiDung) cboLoaiNhanVien.getModel().getSelectedItem();
            Integer idCuaHang = null;
            if (cuaHang != null) {
                idCuaHang = cuaHang.getId();
            }
            Integer idLoaiNguoiDung = null;
            if (loaiNguoiDung != null) {
                idLoaiNguoiDung = loaiNguoiDung.getId();
            }
            nguoiDungs = NguoiDungDAO.TimKiemNhanVien(maNhanVien, txtTenNhanVien.getText().trim(), txtUsername.getText().trim(), txtCMND.getText(), idLoaiNguoiDung, idCuaHang, MainGUI.nguoiDung);
            Utility.CreateColumnsFromDB(NguoiDung.class, nguoiDungs, tbDanhSachNhanVien, new String[]{"Mã nhân viên", "Tên nhân viên", "Username", "CMND", "Điện thoại", "Địa chỉ", "Ngày sinh", "Loại người dùng", "Cửa hàng"}, new String[]{"password"});
            if (nguoiDungs.isEmpty()) {
                JOptionPane.showMessageDialog(this, "Không tìm thấy");
            }
        } catch (Exception ex) {
            logger.error(new Date().toString(), ex);
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_btnTimKiemActionPerformed

    private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
        // TODO add your handling code here:
        Utility.ClearControlInPanel(this);
    }//GEN-LAST:event_btnClearActionPerformed

    private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
        try {
            // TODO add your handling code here:
            NhanVienGUI nhanVienGUI = new NhanVienGUI(null, true, new NguoiDung());
            final int x = (((JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, this)).getWidth() - nhanVienGUI.getWidth()) / 2;
            final int y = (((JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, this)).getHeight() - nhanVienGUI.getHeight()) / 2;
            nhanVienGUI.setLocation(x, y);
            nhanVienGUI.setButtonLuuClickedListener(new MyEventListener() {
                @Override
                public void myEventOccured(Object evt) {
                    NguoiDung nguoiDung = (NguoiDung) evt;
                    if (nguoiDung != null) {
                        nguoiDungs.add(nguoiDung);
                        Utility.CreateColumnsFromDB(NguoiDung.class, nguoiDungs, tbDanhSachNhanVien, new String[]{"Mã nhân viên", "Tên nhân viên", "Username", "CMND", "Điện thoại", "Địa chỉ", "Ngày sinh", "Loại người dùng", "Cửa hàng"}, new String[]{"password"});
                        tbDanhSachNhanVien.changeSelection(tbDanhSachNhanVien.getModel().getRowCount() - 1, 0, false, false);
                    }
                }
            });
            nhanVienGUI.setVisible(true);
        } catch (Exception ex) {
            logger.error(new Date().toString(), ex);
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }

    }//GEN-LAST:event_btnThemActionPerformed

    private void btnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSuaActionPerformed
        // TODO add your handling code here:
        List<NguoiDung> list = (List<NguoiDung>) Utility.GetSelectedRows(tbDanhSachNhanVien, nguoiDungs);
        if (!list.isEmpty()) {
            try {
<<<<<<< .mine
                int index=nguoiDungs.indexOf(list.get(0));
                //SuaKhachHangGUI suaKhachHangGUI=new NhanVienGUI(null, true,list.get(0));
                NhanVienGUI nhanVienGUI=new NhanVienGUI(null, true,list.get(0));
                final int x = (((JFrame)SwingUtilities.getAncestorOfClass(JFrame.class, this)).getWidth() - nhanVienGUI.getWidth()) / 2;
                final int y = (((JFrame)SwingUtilities.getAncestorOfClass(JFrame.class, this)).getHeight() - nhanVienGUI.getHeight()) / 2;
=======
                int index = nguoiDungs.indexOf(list.get(0));
                NhanVienGUI nhanVienGUI = new NhanVienGUI(null, true, list.get(0));
                final int x = (((JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, this)).getWidth() - nhanVienGUI.getWidth()) / 2;
                final int y = (((JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, this)).getHeight() - nhanVienGUI.getHeight()) / 2;
>>>>>>> .r122
                nhanVienGUI.setLocation(x, y);
                nhanVienGUI.setVisible(true);
                Utility.CreateColumnsFromDB(NguoiDung.class, nguoiDungs, tbDanhSachNhanVien, new String[]{"Mã nhân viên", "Tên nhân viên", "Username", "CMND", "Điện thoại", "Địa chỉ", "Ngày sinh", "Loại người dùng", "Cửa hàng"}, new String[]{"password"});
                tbDanhSachNhanVien.changeSelection(index, 0, false, false);
            } catch (Exception ex) {
                logger.error(new Date().toString(), ex);
                JOptionPane.showMessageDialog(this, ex.getMessage());
            }
        } else {
            JOptionPane.showMessageDialog(this, "Xin chọn dữ liệu trên lưới để sửa");
        }
    }//GEN-LAST:event_btnSuaActionPerformed

    private void tbDanhSachNhanVienMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbDanhSachNhanVienMouseClicked
        // TODO add your handling code here:
        if (evt.getClickCount() == 2) {
            btnSuaActionPerformed(null);
        }
    }//GEN-LAST:event_tbDanhSachNhanVienMouseClicked

    private void btnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaActionPerformed
        // TODO add your handling code here:
<<<<<<< .mine
        List<NguoiDung> list=(List<NguoiDung>) Utility.GetSelectedRows(tbDanhSachNhanVien, nguoiDungs);
        
        if(!list.isEmpty()){
=======
        List<NguoiDung> list = (List<NguoiDung>) Utility.GetSelectedRows(tbDanhSachNhanVien, nguoiDungs);
        if (!list.isEmpty()) {
>>>>>>> .r122
            try {
                if (JOptionPane.showConfirmDialog(this, "Bạn có chắc muốn xoá không?", "Question", JOptionPane.YES_NO_OPTION) == 0) {
                    for (NguoiDung nguoiDung : list) {
                        nguoiDung.setIsDelete(true);
                    }
                    NguoiDungDAO.Xoa(list);
                    Utility.RemoveSelectedRows(NguoiDung.class, tbDanhSachNhanVien, nguoiDungs, new String[]{"Mã nhân viên", "Tên nhân viên", "Username", "CMND", "Điện thoại", "Địa chỉ", "Ngày sinh", "Loại người dùng", "Cửa hàng"}, new String[]{"password"});
                    JOptionPane.showMessageDialog(this, "Xoá thành công");
                }
            } catch (Exception ex) {
                logger.error(new Date().toString(), ex);
                JOptionPane.showMessageDialog(this, ex.getMessage());
            }
        } else {
            JOptionPane.showMessageDialog(this, "Xin chọn dữ liệu trên lưới để xoá");
        }
    }//GEN-LAST:event_btnXoaActionPerformed

    private void btnTaiKhoanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTaiKhoanActionPerformed
        // TODO add your handling code here:
        List<NguoiDung> list = (List<NguoiDung>) Utility.GetSelectedRows(tbDanhSachNhanVien, nguoiDungs);
        if (!list.isEmpty()) {
            int index = nguoiDungs.indexOf(list.get(0));
            TaiKhoanGUI taiKhoanGUI = new TaiKhoanGUI(null, true, list.get(0));
            final int x = (((JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, this)).getWidth() - taiKhoanGUI.getWidth()) / 2;
            final int y = (((JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, this)).getHeight() - taiKhoanGUI.getHeight()) / 2;
            taiKhoanGUI.setLocation(x, y);
            taiKhoanGUI.setVisible(true);
            Utility.CreateColumnsFromDB(NguoiDung.class, nguoiDungs, tbDanhSachNhanVien, new String[]{"Mã nhân viên", "Tên nhân viên", "Username", "CMND", "Điện thoại", "Địa chỉ", "Ngày sinh", "Loại người dùng", "Cửa hàng"}, new String[]{"password"});
            tbDanhSachNhanVien.changeSelection(index, 0, false, false);
        } else {
            JOptionPane.showMessageDialog(this, "Xin chọn dữ liệu trên lưới để xem tài khoản");
        }
    }//GEN-LAST:event_btnTaiKhoanActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnSua;
    private javax.swing.JButton btnTaiKhoan;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnTimKiem;
    private javax.swing.JButton btnXoa;
    private javax.swing.JComboBox cboCuaHang;
    private javax.swing.JComboBox cboLoaiNhanVien;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXTable tbDanhSachNhanVien;
    private javax.swing.JTextField txtCMND;
    private javax.swing.JTextField txtMaNhanVien;
    private javax.swing.JTextField txtTenNhanVien;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables
}
